const { toast, loading } = require("../../utils/util")
import {userLogin} from "../../http/api"
// pages/login/login.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    UserName: '',
    Password: '',
    mobile: '',
    vCode: '',
    countdown: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    loading("加载中")
    const code = await getApp().getWxcode()
    const accountInfo = wx.getAccountInfoSync()
    let appid = accountInfo.miniProgram.appId//获取当前程序的appid
    userLogin({
      AppID: appid,
      code
    }).then(res => {
      console.log(res)
      if (res==="success"){
        wx.switchTab({
          url: '/pages/home/home',
        })
      } else {
        toast(res)
      }
    }).catch(err => {
      console.log(err)
      toast(res.data)
    }).finally(()=>{wx.hideLoading()})
  },

  sendCode() {
    let that = this
    if (this.data.mobile.length < 8) {
      return toast("请填写完整手机号")
    }
    that.setData({
      countdown: 60
    })
    let time = 60
    let countdownInterval = setInterval(() => {
      console.log(time)
      that.setData({
        countdown: time--
      })
      if (time < 0) {
        clearInterval(countdownInterval)
      }
    }, 1000)
  },

  async submitLogin() {
    let that = this
    if (!that.data.UserName || !that.data.Password) {
      return toast("请填写账号密码")
    }
    const code = await getApp().getWxcode()
    const accountInfo = wx.getAccountInfoSync()
    let appid = accountInfo.miniProgram.appId//获取当前程序的appid
    userLogin({
      UserName: that.data.UserName,
      Password: that.data.Password,
      AppID: appid,
      code
    }).then(res => {
      console.log(res)
      if (res==="success"){
        wx.redirectTo({
          url: '/pages/home/home',
        })
      } else {
        toast(res)
      }
    }).catch(err => {
      console.log(err)
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})